/***
This figure shows the change in GDP and its components from Q1-2020 to Q2-2020
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category = "Spending"

* Create required subfolders
cap mkdir "${root}/results/Spending"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

*-------------------------------------------------------------------------------
* Load PCE: components of % change
*-------------------------------------------------------------------------------

* Load data
project, uses("${root}/data/derived/NIPA/NIPA Table 2.3.2Q Contributions to Percent Change in Real Personal Consumption Expenditures by Major Type of Product.dta")
use "${root}/data/derived/NIPA/NIPA Table 2.3.2Q Contributions to Percent Change in Real Personal Consumption Expenditures by Major Type of Product.dta", clear

* Keep 2020 Q2
keep if quarter == yq(2020, 2)

* Identify spending categories that appear on credit cards
gen in_affinity = 0
replace in_affinity = 1 if serieslabel == "Furnishings and durable household equipment"
replace in_affinity = 1 if serieslabel == "Recreational goods and vehicles"   
replace in_affinity = 1 if serieslabel == "Other durable goods"
replace in_affinity = 1 if serieslabel == "Food and beverages purchased for off-premises consumption"
replace in_affinity = 1 if serieslabel == "Clothing and footwear"
replace in_affinity = 1 if serieslabel == "Gasoline and other energy goods"
replace in_affinity = 1 if serieslabel == "Other nondurable goods"
replace in_affinity = 1 if serieslabel == "Transportation services"
replace in_affinity = 1 if serieslabel == "Recreation services"
replace in_affinity = 1 if serieslabel == "Food services and accommodations"
replace in_affinity = 1 if serieslabel == "Financial services and insurance"
replace in_affinity = 1 if serieslabel == "Other services"

* Output share of PCE decline that appears on credit cards 
sum value if serieslabel == "Personal consumption expenditures (PCE)"
assert r(N)==1
local pce_decline = r(sum)

sum value if in_affinity == 1
local cc_decline = r(sum)

scalar pce_decline_cc_share = `cc_decline' / `pce_decline'
di pce_decline_cc_share

*------------------------------------------------------------------------------*
* Export stats to paper 
*------------------------------------------------------------------------------*
local pce_decline_cc_share: di %2.0f pce_decline_cc_share * 100
di `pce_decline_cc_share'

cap erase "${root}/results/paper numbers/`category'/Changes in GDP and its Components.yaml"

yamlout using "${root}/results/paper numbers/`category'/Changes in GDP and its Components.yaml", ///
	key("pce_decline_cc_share") ///
	comment("Share of PCE decline in credit card categories") ///
	value(`pce_decline_cc_share') fmt(%2.0f)

*-------------------------------------------------------------------------------
* Load Real GDP: quarterly % change
*-------------------------------------------------------------------------------

* Load data
project, uses("${root}/data/derived/NIPA/NIPA Table 1.1.1Q Percent Change From Preceding Period in Real Gross Domestic Product.dta")
use "${root}/data/derived/NIPA/NIPA Table 1.1.1Q Percent Change From Preceding Period in Real Gross Domestic Product.dta", clear

* Keep 2020 Q2, aggregate GDP only
keep if serieslabel == "Gross domestic product"
keep if quarter == yq(2020, 2)

* Output GDP % decline in 2020q2
assert _N == 1
scalar gdp_pct_decline = value[1]
di gdp_pct_decline

*-------------------------------------------------------------------------------
* Load Real GDP: quarterly levels
*-------------------------------------------------------------------------------

* Load data
project, uses("${root}/data/derived/NIPA/NIPA Table 1.1.6Q Real Gross Domestic Product in Chained Dollars.dta")
use "${root}/data/derived/NIPA/NIPA Table 1.1.6Q Real Gross Domestic Product in Chained Dollars.dta", clear

* Keep desired components for 2020 Q1 & Q2
keep if inlist(serieslabel, "Gross domestic product", "Gross private domestic investment", "Government", "External balance of goods and services", "Personal consumption expenditures")
keep if inrange(quarter, yq(2020, 1), yq(2020, 2))

bys serieslabel (quarter): gen double diff = (value[2] - value[1]) / 1e6 if _n==2
label var diff "Change in GDP Component from 2020 Q1 to Q2, in trillions of chained 2012 dollars"

* Add extra row for credit card spending in PCE
set obs `=_N+1'
replace quarter = yq(2020, 2) in -1
replace serieslabel = "Credit Card Spending in PCE" in -1

sum diff if serieslabel=="Personal consumption expenditures"
assert r(N)==1
replace diff = r(mean) * scalar(pce_decline_cc_share) in -1

*-------------------------------------------------------------------------------
* Plot changes in GDP components during from Q1 to Q2 2020
*-------------------------------------------------------------------------------

* Assign order along x-axis
gen x_axis = .
replace x_axis = 1 if serieslabel == "Gross domestic product"
replace x_axis = 2 if serieslabel == "Gross private domestic investment"
replace x_axis = 3 if serieslabel == "Government"
replace x_axis = 4 if serieslabel == "External balance of goods and services"
replace x_axis = 5 if serieslabel == "Personal consumption expenditures"
replace x_axis = 6 if serieslabel == "Credit Card Spending in PCE"
sort x_axis

label define serieskeys 1 "gdp" 2 "inv" 3 "gov" 4 "exp" 5 "pce" 6 "cc"
label values x_axis serieskeys 

* Generate formatted representations of Q1 to Q2 differences
gen diff_str = strofreal(abs(diff), "%3.2f") if !mi(diff)
replace diff_str = cond(diff>0, "+$", "-$") + diff_str + "T" if !mi(diff)

* Plot
format diff %02.1f
tw ///
	(bar diff x_axis if x_axis == 1, barw(0.6) color(red))  ///
	(bar diff x_axis if inrange(x_axis, 2, 5), barw(0.6) color(gs8)) ///
	(bar diff x_axis if x_axis == 6, barw(0.6) color(ltblue)) ///
	(scatter diff x_axis if diff >= 0 , msymb(none) mlab(diff_str) mlabcolor(gs8) mlabpos(12) mlabsize(medsmall)) ///
	(scatter diff x_axis if diff < 0 , msymb(none) mlab(diff_str) mlabcolor(gs8) mlabpos(6) mlabsize(medsmall)) /// 
	, ///
	xtitle("") ///
	ytitle("Change in Real GDP from Q1 2020 to Q2 2020" "(in trillions of chained 2012 dollars)") ///
	ylab(0 "$0" -0.5 "-$0.5T" -1.0 "-$1.0T" -1.5 "-$1.5T" -2.0 "-$2.0T", nogrid) yline(0, lcolor(gs8)) ///
	xlab( ///
		1 `""Gross" "Domestic" "Product""' ///
		2 `" "Private" "Domestic" "Investment""' ///
		3 `""Government" "Expenditures""' ///
		4 `" "Net" "Exports""' ///
		5 `""Personal" "Consumption" "Expenditures (PCE)" "' ///
		6 `" "Credit Card" "Spending" "in PCE""' ///
		, labsize(medsmall) notick ///
	) ///
	xscale(r(0.7 6)) ///
	legend(off) ///
	text(-1.8 1 "(`=scalar(gdp_pct_decline)'%)", color(gs8) size(medsmall)) ///
	xsize(13)
oi_graph_export "${root}/results/Spending/Components of Change in GDP", type("${fig_type}")

*-------------------------------------------------------------------------------
* Export output numbers to csv file
*-------------------------------------------------------------------------------

foreach cat in "Gross domestic product" "Gross private domestic investment" "Government" "External balance of goods and services" "Personal consumption expenditures" "Credit Card Spending in PCE" {
	disp "`cat'"
	
	sum x_axis if serieslabel == "`cat'"
	assert r(min)==r(max)
	local key : label serieskeys `r(min)'
	
	sum diff if serieslabel == "`cat'"
	assert r(N)==1
	
	local diff_str = strofreal(abs(r(mean)), "%03.2f")
	
	yamlout using "${root}/results/paper numbers/`category'/Changes in GDP and its Components.yaml", ///
		key("`key'_dollars") ///
		comment("Change in `cat' from Q1 2020 to Q2 2020, in trillions of chained 2012 dollars") ///
		value("`diff_str'") fmt(%03.2f)

}

* Absolute value for GDP decline for paper 
scalar gdp_pct_decline = abs(gdp_pct_decline)

yamlout using "${root}/results/paper numbers/`category'/Changes in GDP and its Components.yaml", ///
	key("gdp_percent") ///
	comment("Annualized percent decline in Real GDP from Q1 2020 to Q2 2020") ///
	value(`=scalar(gdp_pct_decline)') fmt(%9.1f)

project, creates("${root}/results/paper numbers/`category'/Changes in GDP and its Components.yaml")
